<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    th, td {
      border: 1px solid #eee;
      padding: 10px 16px;
      text-align: center;
    }

    th {
      background-color: #aaa;
      border-color: #ccc;
    }

    .count {
      margin: 0 5px;
    }
  </style>
</head>
<body>
  <div id="app">app</div>

  <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js" crossorigin></script>

  <script src="./format-utils.js"></script>

  <script type="text/babel">
    
    class App extends React.Component {
      constructor(props) {
        super(props)

        this.state = {
          books: [
            {
              id: 1,
              name: '《Learn React》',
              date: '2020-11',
              price: 128.00,
              count: 1
            },
            {
              id: 2,
              name: '《Learn Vue》',
              date: '2020-11',
              price: 248.00,
              count: 1
            },
            {
              id: 3,
              name: '《Learn Learvel》',
              date: '2020-11',
              price: 328.00,
              count: 1
            },
            {
              id: 4,
              name: '《Learn Flutter》',
              date: '2020-11',
              price: 108.00,
              count: 1
            }
          ]
        }
      }

      render() {
        return (
          <div>
            <table style={{border: '1px solid #eee', borderCollapse: 'collapse'}}>
              <thead>
                <tr>
                  <th></th>
                  <th>书籍名称</th>
                  <th>出版日期</th>
                  <th>价格</th>
                  <th>购买数量</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                {
                  this.state.books.map((item, index) => {
                    return (
                      <tr>
                        <td>{index + 1}</td>
                        <td>{item.name}</td>
                        <td>{item.date}</td>
                        <td>{formatPrice(item.price)}</td>
                        <td>
                          <button>-</button>
                          <span className="count">{item.count}</span>
                          <button>+</button>
                        </td>
                        <td>
                          <button>移除</button>
                        </td>
                      </tr>
                    )
                  })
                }
              </tbody>  
            </table>
            <h2>总价格: {this.getTotalPrice()}元</h2>
          </div>
        )
      }

      getTotalPrice() {
        // 1.for循环遍历
        // let totalPrice = 0
        // for (let item of this.state.books) {
        //   totalPrice += item.price * item.count
        // }
        // return formatPrice(totalPrice)

        // 2. fillter/map/reduce(归纳为)
        const totalPrice = this.state.books.reduce((preValue, item, index) => {
          return preValue + item.price * item.count
        }, 0)
        return formatPrice(totalPrice)
      }
    }

    ReactDOM.render(<App />, document.getElementById('app'))
  </script>
</body>
</html>